package com.showcai.controller.watchEnroll;

import com.github.pagehelper.PageInfo;
import com.showcai.controller.base.BaseController;
import com.showcai.def.Constant;
import com.showcai.module.ApplyWatch;
import com.showcai.module.ExpressInfo;
import com.showcai.module.RefundApply;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.result.ResultObj;
import com.showcai.result.ReturnCode;
import com.showcai.service.*;
import com.showcai.util.ExcelIn;
import com.showcai.util.ExcelOut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Baggio on 2018-9-6.
 */
@RequestMapping("/refund")
@Controller
public class RefundApplyController extends BaseController {

    @Autowired
    RefundApplyService refundApplyService;
    @Autowired
    ApplyService applyWatchService;
    @Autowired
    OrderService orderService;
    /**
     * 查询所有退款用户
     *
     * */
    @RequestMapping(value = "/refundApplyList")
    public ModelAndView refundApplyList(Integer page, Integer pageSize, RefundApply refundApply) {
        ModelAndView mov = new ModelAndView();
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        Map<String,String> map=super.showRequestParameters();
        map.put("page",String.valueOf(page));
        map.put("pageSize",String.valueOf(pageSize));
        PageInfo list = refundApplyService.selectRefundApplyPageList(map);
        mov.addObject("pageInfo",list);
        mov.addObject("bean",map);
        mov.setViewName("/watchEnroll/refundApplyList");
        return mov;
    }
    /**
     * 同意退款申请
     *
     * */
    @RequestMapping(value = "/agreeRefundApply")
    @ResponseBody
    public String agreeRefundApply(Long orderId) {
        ResultObj resultObj = new ResultObj();
        try {
            ApplyWatch applyWatch = (ApplyWatch)applyWatchService.selectApplyByOrderId(orderId);
            applyWatch.setStatus(OrderService.REFUNDED);
            applyWatchService.modifyApply(applyWatch);
            orderService.modifyOrderStatus(orderId,OrderService.REFUNDED);
            refundApplyService.agreeRefundApply(orderId,OrderService.REFUNDED);
        } catch (MyRunTimeExcption e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(ReturnCode.FAIL.getMsg());
            e.printStackTrace();
        }catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(ReturnCode.FAIL.getMsg());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }
    @RequestMapping(value = "/refundApplyExcelOut")
    @ResponseBody
    public void refundApplyExcelOut() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map<String,String> map=super.showRequestParameters();
        String[] strList = {"姓名","电话","订单ID","付款时间","退款原因","订单状态","创建时间"};
        List<RefundApply> refundApplyList = refundApplyService.selectRefundApplyList(map);
        String fileName = "refundApply"+ new SimpleDateFormat("yyyyMMdd").format(new Date());
        List<LinkedList<String>> list = new ArrayList<>();
        for (RefundApply refundApply : refundApplyList) {
            LinkedList<String> lmap = new LinkedList<>();
            lmap.add(refundApply.getUserName());
            lmap.add(refundApply.getPhone());
            lmap.add(String.valueOf(refundApply.getOrderId()));
            if(refundApply.getApplyTime()!=null){
                lmap.add(sdf.format(refundApply.getApplyTime()));
            }else{
                lmap.add("");
            }
            lmap.add(refundApply.getRefundCause());
            if(refundApply.getStatus().byteValue()==OrderService.REFUND.byteValue()){
                lmap.add("申请退款");
            }else if(refundApply.getStatus().byteValue()==OrderService.REFUNDED.byteValue()){
                lmap.add("已退款");
            }else{
                lmap.add("");
            }
            if(refundApply.getCreateTime()!=null){
                lmap.add(sdf.format(refundApply.getCreateTime()));
            }else{
                lmap.add("");
            }
            list.add(lmap);
        }
        ExcelOut.excelOut(fileName,response,strList,list);
    }
}
